/**
 * 打印设置
 */
function scPrintSetting(){
    var opt = null;
    var com = scCommon.instance();
    var crEle = com.crEle;

    /**
     * 条码属性
     */
    var list = [
        { 
            label: "指定页：", value: "pageIndex", type: "input", placeholder: "填入页码，多个按逗号分隔",
            checkEnable: true, checkOption: {name: "fixPageIndexes", checked: true}
        },
        {
            label: "起始页：", type: "component", component: "scNumber",
            param: {name: "startPageIndex", minNum: 1, maxNum: 10000, value: 1}
        },
        {
            label: "起始行：", type: "component", component: "scNumber",
            param: {name: "startRowIndex", minNum: 1, maxNum: 1000, value: 1}
        },
        {
            label: "结束页：", type: "component", component: "scNumber",
            param: {name: "endPageIndex", minNum: 1, maxNum: 10000, value: 1}
        },
        {
            label: "结束行：", type: "component", component: "scNumber",
            param: {name: "endRowIndex", minNum: 1, maxNum: 1000, value: 1}
        },
        {
            label: "背景选项：",
            groups: [
                {label: "打印水印", value: "watermark", type: "checkbox", checked: true, defaultVal: true},
                {label: "打印底纹", value: "texture", type: "checkbox", checked: true, defaultVal: true},
            ]
        },
        {
            label: "页面选项：",
            groups: [
                {label: "打印页眉", value: "pageHead", type: "checkbox", checked: true, defaultVal: true},
                {label: "打印页脚", value: "pageFoot", type: "checkbox", checked: true, defaultVal: true},
            ]
        }
        
    ];

    var box = null;

    /**
     * 创建视图
     * @returns 
     */
    this.createView = function(){
        box = crEle(null, "sc-print-setting");

        let rowClazz = "sc-print-setting-row";
        let disableRows = null;
        let disabled = false;

        let excludes = ["watermark", "texture", "pageHead", "pageFoot"];

        com.insertNode(list, {}, box, rowClazz, (node, input, row)=>{
            if(node.name == "fixPageIndexes"){
                disabled = node.checked;
                disableRows = (flg)=>{
                    let rowList = box.querySelectorAll("." + rowClazz);
                    for(let i = 0; i < rowList.length; i++){
                        if(row === rowList[i]) continue;
                        let inputs = rowList[i].querySelectorAll("input");
                        for(let j = 0; j < inputs.length; j++){
                            if(excludes.indexOf(inputs[j].name) > -1) continue;
                            inputs[j].disabled = flg;
                        }
                    }
                }

                node.addEventListener("change",(e)=>{
                    if(e.currentTarget.checked){
                        e.currentTarget.value = true;
                        input.disabled = false;
                        disableRows(true);
                    }else{
                        e.currentTarget.value = false;
                        input.disabled = true;
                        disableRows(false);
                    }
                });
            }
        });

        disableRows(disabled);

        return box;
    }

    /**
     * 获取数据
     * @returns 
     */
    this.getData = function(){
        return new Promise((resolve)=>{
            com.getData(box).then((dt)=>{
                let data = {
                    type: "continue"
                };

                if(dt.fixPageIndexes){
                    let pageRange = [];
                    dt.pageIndex.split(",").forEach(val=>{
                        pageRange.push(parseInt(val));
                    });
                    data.pageRange = pageRange;
                }else{
                    data.startPageIndex = parseInt(dt.startPageIndex);
                    data.startRowIndex = parseInt(dt.startRowIndex);
                    data.endPageIndex = parseInt(dt.endPageIndex);
                    data.endRowIndex = parseInt(dt.endRowIndex);
                }

                data.watermark = dt.watermark;
                data.texture = dt.texture;
                data.pageHead = dt.pageHead;
                data.pageFoot = dt.pageFoot;

                resolve(data);
            });
        });
    }
}